<?php
/**
 * Created by PhpStorm.
 * User: wangye
 * Date: 19-2-14
 * Time: 下午5:27
 */

//?id=
function api($url,$pattems=array(),$method = 'GET',$header=array()){
    $opts = array(
        'CURLOPT_TIMEOUT' => 30,
        "CURLOPT_RETURNTRANSFER" => TRUE,
        "CURLOPT_SSL_VERIFYPEER" => FALSE,
        "CURLOPT_SSL_VERIFYHOST" => FALSE,
        "CURLOPT_HEADER" => FALSE,
        "CURLOPT_HTTPHEADER" => $header
    );

    switch (strtoupper($method)){
        case "GET":
            $opts[CURLOPT_URL] = $url.'?'.http_build_query($pattems);
            break;
        case "POST":
            $opts[CURLOPT_URL] = $url;
            $opts[CURLOPT_POST] = TRUE;
            $opts[CURLOPT_POSTFIELDS] = $pattems;
            break;
    }
    $ch = curl_init();
    curl_setopt_array($ch,$opts);
    $res = curl_exec($ch);
    $error = curl_error($ch);
    curl_close();
    if ($error){
        die("脚步执行出错");
    }
    return $res;
}
$url = 'http://localhost:63342/github/MyNodeBook/curl/demo2/put.php';
$pattems = array('id'=>2);
$ret = api($url,$pattems);
echo $ret;